# 配置管理
from typing import List, Union

from openai import AsyncAzureOpenAI, AzureOpenAI, OpenAI
API_KEY = "1KHm8zFftbDwil33cdFOTvBZo3DEaaPQujH6ijY5AmhgeBQQ0dn2JQQJ99BGACMsfrFXJ3w3AAABACOGqalv"
AZURE_ENDPOINT = "https://starit-openai.openai.azure.com/"
API_VERSION = "2024-12-01-preview"
client_azure = AzureOpenAI(
    api_key=API_KEY,
    api_version=API_VERSION,
    azure_endpoint=AZURE_ENDPOINT,
)


def get_embedding(
    text,
):
    response = client_azure.embeddings.create(
        model="text-embedding-ada-002", input=text
    )
    return response.data[0].embedding


messages = [{"role": "system", "content": "你是一个 helpful 的助手。"}]

while True:
    user_input = input("你: ")
    if user_input.lower() in ["退出", "quit", "exit"]:
        print("聊天结束。")
        break

    messages.append({"role": "user", "content": user_input})

    print("AI: ", end="", flush=True)
    full_response = ""
    stream = client_azure.chat.completions.create(
        model="gpt-4o-mini", messages=messages, stream=True
    )

    print(stream)
    # print(stream.choices[0].message.content)

    for chunk in stream:
        try:
            if chunk.choices[0].delta.content is not None:
                content = chunk.choices[0].delta.content
                print(content, end="", flush=True)
                full_response += content
        except:
            print("空数据包")

    print()
    messages.append({"role": "assistant", "content": full_response})
